/* stroke handlers
 * the first parameter is the tab object in which the stroke is generated
 * the second parameter is the path object recorded in the stroke.js */
var _actions = {
	'to-page-top': function(tab, path) {
		document.body.scrollTop = 0;
	},

	'to-page-bottom': function(tab, path) {
		document.body.scrollTop = document.body.scrollHeight;
	},

        'scroll-up-one-page': function(tab, path) {
		//document.body.scrollByPages(-1);
		document.body.scrollTop -= (window.innerHeight - 20);
	},

        'scroll-down-one-page': function(tab, path) {
		//document.body.scrollByPages(1);
		document.body.scrollTop += (window.innerHeight - 20);
	},

	'history-back': function(tab, path) {
		history.back();
	},

	'history-forward': function(tab, path) {
		history.forward();
	},

	'previous-tab': function(tab, path) {
		_actions['next-tab'](tab, path, -1);
	},

	'next-tab': function(tab, path, dir) {
		var i, n, step = (!!dir && dir < 0) ? -1 : 1;
		chrome.tabs.getAllInWindow(null, function(tabs) {
			n = tabs.length;
			for (i = 0; i < n; ++i) {
				if (tabs[i].id == tab.id) {
					if (!!(tab = tabs[(i+step+n)%n])) {
						chrome.tabs.update(tab.id, {selected: true});
					}
					break;
				}
			}
		});
	},

        'upper-level-in-url': function(tab, path) {
		var p = location.protocol + '//' + location.host +
			location.pathname.slice(0, -1).replace(/[^\/]*$/, '');
		if (p != location.href) location.href = p;
	},

        'increase-number-in-url': function(tab, path, i) {
		var n = location.href.match(/(\d+)$/);
		if (n) {
			n = Math.max(0, parseInt(n[1]) + (isNaN(i) ? 1 : i));
			location.href = location.href.replace(/\d+$/, n);
		}
	},

        'decrease-number-in-url': function(tab, path) {
		_actions['increase-number-in-url'](tab, path, -1);
	},

	'minimize-window': function(tab, path) {
		/* not implemented */
	},

	'maximize-window': function(tab, path) {
		chrome.windows.getCurrent(function(wnd) {
			chrome.windows.update(wnd.id, { left: 0, top: 0, width: screen.width, height: screen.height });
		});
	},

	'new-window': function(tab, path) {
		chrome.windows.create();
	},

	'close-window': function(tab, path) {
		chrome.windows.getCurrent(function(wnd) {
			chrome.windows.remove(wnd.id);
		});
	},

	'new-tab': function(tab, path) {
		chrome.tabs.create({
			index: tab.index + 1,
			selected: true
		});
	},

	'close-tab': function(tab, path) {
		chrome.tabs.remove(tab.id);
	},

	'undo-close-tab': function(tab, path) {
		if (0 < _tabs.closed.length) {
			var dead = _tabs.all[_tabs.closed.pop()];
			chrome.tabs.create({
				index: tab.index + 1,
				selected: true,
				url: dead.url
			}, function() {
				/* a new tab is created (same url, different tab id),
				 * the old tab has long gone */
				delete _tabs.all[dead.id];
			});
		}
	},

	'detach-tab': function(tab, path) {
		chrome.windows.create(null, function(wnd) {
			chrome.tabs.move(tab.id, { windowId: wnd.id, index: 0 });
			/* remove all other tabs */
			chrome.tabs.getAllInWindow(wnd.id, function(tabs) {
				for (var i = 0; i < tabs.length; ++i) {
					if (tabs[i].id != tab.id) {
						chrome.tabs.remove(tabs[i].id);
					}
				}
			});
		});
	},

	'duplicate-tab': function(tab, path) {
		chrome.tabs.create({
			index: tab.index + 1,
			url: tab.url
		});
	},

	'close-other-tabs': function(tab, path) {
		chrome.tabs.getAllInWindow(null, function(tabs) {
			for (var i = 0; i < tabs.length; ++i) {
				if (tabs[i].id != tab.id) {
					chrome.tabs.remove(tabs[i].id);
				}
			}
		});
	},

        'open-link-in-new-window': function(tab, path) {
		if (!!path.mousedown.srcElement.href) {
			chrome.windows.create({
				url: path.mousedown.srcElement.href
			});
		}
	},

        'open-link-in-new-background-tab': function(tab, path) {
		if (!!path.mousedown.srcElement.href) {
			chrome.tabs.create({
				index: tab.index + 1,
				url: path.mousedown.srcElement.href,
				selected: false
			});
		}
	},

        'open-link-in-new-foreground-tab': function(tab, path) {
		if (!!path.mousedown.srcElement.href) {
			chrome.tabs.create({
				index: tab.index + 1,
				url: path.mousedown.srcElement.href,
				selected: true
			});
		}
	},

        'bookmark-this-link': function(tab, path, url, title) {
		url = url || path.mousedown.srcElement.href;
		title = title || path.mousedown.srcElement.innerText;

		if (!url) return;

		chrome.bookmarks.getChildren('0', function(root) {
			/* add to bookmarks bar */
			chrome.bookmarks.create({
				parentId: root[0].id,
				title: title,
				url: url
			}, function() {
				/* TODO: some notifications */
			});
		});
	},

        'view-image': function(tab, path) {
		if (!!path.mousedown.srcElement.src) {
			chrome.tabs.create({
				index: tab.index + 1,
				url: path.mousedown.srcElement.src,
				selected: true
			});
		}
	},

        'save-image': function(tab, path) {
		/* not implemented */
	},

	'bookmark-this-page': function(tab, path) {
		_actions['bookmark-this-link'](tab, path, tab.url, tab.title);
	},

	'remove-bookmark': function(tab, path) {
		/* search by host, full url sometimes won't return results even
		 * when there are exact matches */
		var host = $create('a', { href: tab.url }).host;
		chrome.bookmarks.search(host, function(bks) {
			for (var i = 0; i < bks.length; ++i) {
				if (bks[i].url == tab.url) {
					chrome.bookmarks.remove(bks[i].id);
				}
			}
		});
	},

	'reload': function(tab, path) {
		location.reload();
	},

	'skip-cache-reload': function(tab, path) {
		location.reload(true);
	},

	'stop-page-loading': function(tab, path) {
		window.stop();
	},

	'view-source': function(tab, path) {
		chrome.tabs.create({
			url: 'view-source:' + tab.url
		});
	},

	'take-screenshot': function(tab, path) {
		chrome.tabs.captureVisibleTab(null, function(url) {
			chrome.tabs.create({
				index: tab.index + 1,
				selected: true,
				url: url
			});
		});
	},

	'text-zoom-in': function(tab, path) {
		_actions['text-zoom-reset'](tab, path, 0.01);
	},
	'text-zoom-out': function(tab, path) {
		_actions['text-zoom-reset'](tab, path, -0.01);
	},

	/* zoom text */
	'text-zoom-reset': function(tab, path, step) {
		if (!document.zoom || isNaN(step)) document.zoom = 1;

		var zoom = document.zoom += isNaN(step) ? 0 : step;
		var css = document.getElementById('ms-zoom-' + tab.id);

		if (1 == zoom) {
			if (!!css) document.head.removeChild(css);
		} else {
			if (!css) {
				css = document.createElement('style');
				css.id = 'ms-zoom-' + tab.id;
				css.type = 'text/css';
				document.head.appendChild(css);
			}
			css.innerText = '* { font-size: ' + zoom + 'em !important; }';
		}
	},

	'search-selected-text': function(tab, path) {
		if (!path.seltext) return;
		chrome.tabs.create({
			index: tab.index + 1,
			selected: true,
			url: 'http://www.google.com/search?q=' + path.seltext
		});
	},

	'mouse-stroke-options': function(tab, path) {
		chrome.tabs.create({
			url: OPTION_URL
		});
	}
};

/* declare functions which need to be execute locally in the content script */
_actions['to-page-top'].local =
_actions['to-page-bottom'].local =
_actions['scroll-up-one-page'].local =
_actions['scroll-down-one-page'].local =
_actions['history-back'].local =
_actions['history-forward'].local =
_actions['upper-level-in-url'].local =
_actions['increase-number-in-url'].local =
_actions['decrease-number-in-url'].local =
_actions['reload'].local =
_actions['skip-cache-reload'].local =
_actions['stop-page-loading'].local =
_actions['text-zoom-in'].local =
_actions['text-zoom-out'].local =
_actions['text-zoom-reset'].local =
true;